package org.jeecg.modules.device;

import lombok.extern.slf4j.Slf4j;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
import org.jeecg.modules.driver.DriverCommandList;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class PowerDevice extends Device {

    @Override
    public void online(Boolean isManual) {
//        this.onStatus = 1+"";
        try {
//            shadow.getJSONObject("baseinfo").put("isOnline",1);
//            shadow.put("onTime", Calendar.getInstance().getTime().getTime());
            if (isManual) {
                shadow.put("manualDisconnect", 0);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void offLine(Boolean isManual) {
        this.onStatus = 0 + "";
        try {
            shadow.getJSONObject("baseinfo").put("isOnline", 0);
            shadow.put("offTime", Calendar.getInstance().getTime().getTime());
            if (isManual) {
                shadow.put("manualDisconnect", 1);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Boolean login() {
        return this.deviceDriver.runCommand(DriverCommandList.LOGIN, null);
    }

    @Override
    public String getVersion() {
        return "1";
    }

    /**
     * 有通道报警值变化时返回
     *
     * @return
     */
    @Override
    public String getNewChannelWarning() {
        try {
            JSONObject oldData = getOldData();
            String msg = "";
            if (oldData != null) {
                //JSONArray oldArray = oldData.getJSONObject("switches").getJSONArray("switches");
                JSONArray arrData = shadow.getJSONObject("switches").getJSONArray("switches");
                for (int i = 0; i < arrData.length(); i++) {
                    //int oldIswarning = oldArray.getJSONObject(i).getInt("iswarning");
                    //if(oldIswarning == 0) {
                    int curIswarning = arrData.getJSONObject(i).getInt("iswarning");
                    if (curIswarning == 1) {
                        Double cur = Double.parseDouble(arrData.getJSONObject(i).getString("current"));
                        Double cc = Double.parseDouble(arrData.getJSONObject(i).getString("controlC"));
                        msg += "通道开关" + String.valueOf(i + 1) +"【" + arrData.getJSONObject(i).getString("name") +  "】"+ "当前告警值:" + cur + " 设置预警值:" + cc;
                    }
                    //}
                }
            }
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 有报警值变化时返回报警内容
     *
     * @return
     */
    @Override
    public Map<Integer, String> getNewDeviceWarning() {
        try {
            JSONObject oldData = getOldData();
            String msg = "";
            String object = "warning";
            String[] keys = {"cityVol", "mainVol", "backVol", "current", "ups", "temp"};
            Map<Integer, String> warnMsg = new HashMap<>();
            String[] wanringMsg = {"市电异常", "请检查",
                    "主电异常", "当前电压" + shadow.getJSONObject("baseinfo").getString("mainVol") + "V",
                    "备电异常", "当前电压" + shadow.getJSONObject("baseinfo").getString("backVol") + "V",
                    "电流异常", "当前电流" + shadow.getJSONObject("baseinfo").getString("supplyCurrent") + "A",
                    "UPS异常", "请检查",
                    "温度异常", "当前温度" + shadow.getJSONObject("baseinfo").getString("temp") + "°C"};
            if (oldData != null) {
                for (int i = 0; i < keys.length; i++) {
                    //if (!oldData.getJSONObject(object).isNull(keys[i])) {
                    Object oldStateObj = oldData.getJSONObject(object).get(keys[i]);
                    if (oldStateObj instanceof Integer){
                        //log.info("if===getNewDeviceWarning=====Integer");
                        Integer oldState = (Integer)oldStateObj;
                        if (oldState == 0) {
                            //log.info("if===getNewDeviceWarning=====oldState == 0");
                            if (oldState < shadow.getJSONObject(object).getInt(keys[i])) {
                                //log.info("if===getNewDeviceWarning=====oldState <<<<<<<<<");
                                msg = wanringMsg[i * 2] + "," + wanringMsg[i * 2 + 1] + ";";
                                warnMsg.put(i, msg);
                            }
                        }
                        //log.info("if===getNewDeviceWarning====="+keys[i]+":=====" + oldData.getJSONObject(object).getString(keys[i]));
                    }

                  //  }
                }
            }
            return warnMsg;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
